//
//  GYXTextView.swift
//  GYXWeibo
//
//  Created by Twisal on 2017/4/6.
//  Copyright © 2017年 Twisal. All rights reserved.
//

import UIKit


// MARK: - 自定义textView
class GYXTextView: UITextView {
    /// 懒加载占位label
    lazy var placeHolderLabel = UILabel()
    
    override func awakeFromNib() {
        
        setupUI()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc fileprivate func textChange(note: Notification) {
        placeHolderLabel.isHidden = self.hasText
    }
}


// MARK: - 视图布局
private extension GYXTextView {
    
    /// 布局视图
    func setupUI()  {
        // 添加通知
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(textChange),
                                               name: NSNotification.Name.UITextViewTextDidChange,
                                               object: nil)
        // 设置占位文字label的属性
        placeHolderLabel.text = "分享新鲜事..."
        placeHolderLabel.font = self.font
        placeHolderLabel.textColor = UIColor.lightGray
        placeHolderLabel.frame.origin = CGPoint(x: 5, y: 8)
        placeHolderLabel.sizeToFit()
        
        // 添加占位Label
        addSubview(placeHolderLabel)
    }
}
